package com.zq.vaccinum.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zq.vaccinum.domain.JsonResult;
import com.zq.vaccinum.domain.LoginBody;
import com.zq.vaccinum.domain.entity.User;
import com.zq.vaccinum.domain.entity.UserInfo;
import com.zq.vaccinum.service.IUserInfoService;
import com.zq.vaccinum.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author zq
 * @since 2023-05-27
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{
    @Autowired
    IUserService userService;
    @Autowired
    IUserInfoService userInfoService;
    //定义json解析工具
    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 用户登录接口
     * @param loginBody
     * @return
     */
    @PostMapping("login")
    public JsonResult login(@RequestBody LoginBody loginBody){
        String token = userService.login(loginBody.getUsername(), loginBody.getPassword(),
                loginBody.getCode(), loginBody.getUuid());
        return new JsonResult(OK,"登录成功")
                .put("token",token);
    }


    @PostMapping("register")
    public JsonResult register(@RequestBody User user) throws JsonProcessingException {
        userService.register(user);
        return new JsonResult(OK);
    }


    //查询手机号码是否存在的接口
    @RequestMapping("queryPhone")
    public String queryPhone(String phone) throws JsonProcessingException {
        //定义一个map来存放json的键值对数据
        HashMap result = new HashMap();
        //通过调用业务层来实现业务功能-查询数量
        int num = userService.count(new QueryWrapper<User>().eq("phone", phone));
        //在结果中存放
        result.put("exits", num > 0);
        //返回json
        return objectMapper.writeValueAsString(result);
    }

    @GetMapping("list")
    @PreAuthorize("hasAuthority('sys:user:list')")
    public String list(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,//页码
            @RequestParam(required = false, defaultValue = "4") Integer pageSize,//每页显示条数
            @RequestParam(required = false, defaultValue = "") String keyword//查询关键字-身份证
        ) throws JsonProcessingException {


        // key-vlue的map集合
        HashMap result = new HashMap();
        // 通过page进行分页查询【当前页、每页显示的记录条数】
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>().like(keyword != null && !keyword.equals(""), "name", keyword);
        Page page = userService.page(
                new Page<User>(pageNum, pageSize),
                queryWrapper
        );
        // 从page中获取list集合数据
        List<User> list = page.getRecords();
        //  保存到结果中
        result.put("list", list);
        result.put("total", page.getTotal());
        //把集合解析为json格式的字符串，返回
        return objectMapper.writeValueAsString(result);
    }

    // 用户账号+信息
    @RequestMapping("insert")
    public JsonResult insert(User user, UserInfo userInfo) throws JsonProcessingException {
        // 更新用户信息
        boolean userSave = userService.save(user);
        // 更新用户详细信息
        userInfo.setUserId(user.getId());
        boolean userInfoSave = userInfoService.save(userInfo);
        return new JsonResult(OK);
    }

    /**
     * 逻辑删除
     * @param user
     * @return
     */
    @RequestMapping("/softDelete")
    public JsonResult delete(User user) throws JsonProcessingException {
        userService.softDelete(user);
        return new JsonResult(OK);
    }

    @RequestMapping("logout")
    public JsonResult logout(){
        userService.logout();
        return new JsonResult(OK,"注销成功");
    }

    @GetMapping("getInfo")
    public JsonResult getInfo(String username){
        User user = userService.getUserInfo(username);
        return new JsonResult(OK,"用户信息获取成功",user);
    }

    @RequestMapping("/update")
    public JsonResult update(User user) throws JsonProcessingException {
        boolean update = userService.updateById(user);
        return new JsonResult(OK);
    }
}
